package com.lala.controller;

import com.zhenzi.sms.ZhenziSmsClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.TimeUnit;

@Controller
@RequestMapping("/Verification")
public class VerificationController {
    @Autowired
    RedisTemplate redisTemplate;
    @GetMapping("/code")
    @ResponseBody
    public String code(String number, HttpServletRequest request){
      //HttpSession session= request.getSession();
        //生成6位验证码
        String verifyCode = String.valueOf(new Random().nextInt(899999) + 100000);
       // session.setAttribute("code",verifyCode);
        redisTemplate.opsForValue().set("code",verifyCode);
        redisTemplate.expire("code",1, TimeUnit.MINUTES);
        //System.out.println(session.getAttribute("code"));
        System.out.println(verifyCode);
        //发送短信
        ZhenziSmsClient client = new ZhenziSmsClient("https://sms_developer.zhenzikj.com","104918","41b3226f-dc74-4a90-b9bd-c622bd19de28");
        Map map=new HashMap();
        map.put("message","验证码为:"+verifyCode);
        map.put("number",number);
        try {
          String msg=  client.send(map);
            System.out.println(msg);
            return "success";
        } catch (Exception e) {
            e.printStackTrace();
            return "fail";
        }



    }


}
